Imports System
Imports System.Web

<ParseChildren(True, "VirtualPages")> _
Public Class NavigationMap
    Inherits Panel

    Private m_lstVirtualPages As New List(Of VirtualPage)
    Private m_lstVirtualPageControls As New List(Of IVirtualPage)
    Private m_lstNavigationButtons As New List(Of AnchorButton)

    Private m_iStartStep As Integer = 1 '1 Based
    Private m_sbInitStartStep As New Text.StringBuilder
    Private m_sDividerCssClass As String

    Private m_ctlButtonCssClass As New Variable
    Private m_ctlCurrentButtonCssClass As New Variable

    Private m_ctlHideListener As New Listener

    <PersistenceMode(PersistenceMode.InnerDefaultProperty)> _
    Public ReadOnly Property VirtualPages() As List(Of VirtualPage)
        Get
            Return m_lstVirtualPages
        End Get
    End Property

    Public Property StartStep() As Integer
        Get
            Return m_iStartStep
        End Get
        Set(ByVal value As Integer)
            m_iStartStep = value
        End Set
    End Property

    Public Property DividerCssClass() As String
        Get
            Return m_sDividerCssClass
        End Get
        Set(ByVal value As String)
            m_sDividerCssClass = value
        End Set
    End Property

    Public Property ButtonCssClass() As String
        Get
            Return m_ctlButtonCssClass.Value
        End Get
        Set(ByVal value As String)
            m_ctlButtonCssClass.Value = value
        End Set
    End Property

    Public Property CurrentButtonCssClass() As String
        Get
            Return m_ctlCurrentButtonCssClass.Value
        End Get
        Set(ByVal value As String)
            m_ctlCurrentButtonCssClass.Value = value
        End Set
    End Property

    Public Sub New()
        MyBase.New()

        With m_ctlButtonCssClass
            .Name = "_buttonCssClass"
            .Type = Variable.DataType.String
        End With

        With m_ctlCurrentButtonCssClass
            .Name = "_currentButtonCssClass"
            .Type = Variable.DataType.String
        End With

        With m_ctlHideListener
            .Event = "HideNavigationMap"
        End With

        Constructor = "new jObjective.NavigationMap($('#" & DataID & "'))"
    End Sub

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        With Controls
            .Add(m_ctlButtonCssClass)
            .Add(m_ctlCurrentButtonCssClass)
            .Add(m_ctlHideListener)
        End With

        ChildControlsCreated = True
    End Sub

    Private Sub NavigationMap_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        For i As Integer = 0 To VirtualPages.Count - 1
            Dim objVirtualPage As VirtualPage = VirtualPages(i)

            Dim ctlNavigationButton As New AnchorButton
            With ctlNavigationButton
                .Text = objVirtualPage.Title
                .Style("margin-right") = "0px"
                .CssClass = ButtonCssClass
                .ObjectName = "Page" & i & "Button"
            End With

            Controls.Add(ctlNavigationButton)
            m_lstNavigationButtons.Add(ctlNavigationButton)

            If i <> VirtualPages.Count - 1 Then
                Dim ctlDivider As New WebControls.WebControl(HtmlTextWriterTag.Div)
                With ctlDivider
                    .CssClass = m_sDividerCssClass
                End With

                Controls.Add(ctlDivider)
            End If
        Next
    End Sub

    Private Sub NavigationMap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i As Integer = 0 To VirtualPages.Count - 1
            Dim objVirtualPage As VirtualPage = VirtualPages(i)

            Dim objCurrentPage As IVirtualPage = Me.FindControl(objVirtualPage.Control)

            With DirectCast(objCurrentPage, WebControls.WebControl)
                If .Enabled = False Then
                    m_lstNavigationButtons(i).Enabled = False
                    m_lstNavigationButtons(i).Style("display") = "none"
                    DirectCast(Controls((i * 2) + 4), WebControls.WebControl).Style("display") = "none"
                Else
                    If StartStep - 1 = i Then
                        With m_sbInitStartStep
                            .Append("window.onload=function() {")
                            .Append([Namespace])
                            .Append(".transition(")
                            .Append(objCurrentPage.Namespace)
                            .AppendLine(");}")
                        End With
                    End If

                    If i > 0 Then
                        Dim objPrevious As WebControls.WebControl = Me.FindControl(VirtualPages(i - 1).Control)
                        Dim objPreviousPage As IVirtualPage = DirectCast(objPrevious, IVirtualPage)

                        If objPrevious.Enabled Then
                            objCurrentPage.PreviousPage = [Namespace] & ".transition(" & objPreviousPage.Namespace & "," & i & "); return false;"
                        Else
                            objCurrentPage.PreviousPage = Nothing
                        End If
                        objPreviousPage.NextPage = [Namespace] & ".transition(" & objCurrentPage.Namespace & "," & i & "); return false;"

                        If i = VirtualPages.Count - 1 Then
                            objCurrentPage.NextPage = Nothing
                        End If
                    End If

                    Dim objPage As New Variable
                    With objPage
                        .Name = "Page" & i
                        .Value = objCurrentPage.Namespace
                    End With

                    m_lstNavigationButtons(i).OnClientClick = [Namespace] & ".transition(" & objCurrentPage.Namespace & "," & i & ");"

                    If StartStep > i + 1 Then
                        m_lstNavigationButtons(i).Enabled = True
                    End If

                    Controls.Add(objPage)
                End If
            End With
        Next
    End Sub

    Private Sub NavigationMap_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        With m_ctlHideListener
            .Handler = [Namespace] & ".hide"
        End With

        ScriptManager.RegisterStartupScript(Page, Me.GetType, "InitStartStep", m_sbInitStartStep.ToString, True)
    End Sub
End Class

<ControlBuilder(GetType(VirtualPageBuilder))> _
Public Class VirtualPage
    Implements IAttributeAccessor

    Private m_objAttributes As AttributeCollection = Nothing
    Private m_sVirtualPage As String
    Private m_sTitle As String

    <PersistenceMode(PersistenceMode.Attribute), IDReferenceProperty(GetType(IVirtualPage)), ComponentModel.TypeConverter(GetType(WebControls.ControlIDConverter))> _
    Public Property Control() As String
        Get
            Return m_sVirtualPage
        End Get
        Set(ByVal value As String)
            m_sVirtualPage = value
        End Set
    End Property

    <PersistenceMode(PersistenceMode.Attribute)> _
    Public Property Title() As String
        Get
            Return m_sTitle
        End Get
        Set(ByVal value As String)
            m_sTitle = value
        End Set
    End Property

    <ComponentModel.Browsable(False), ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public ReadOnly Property Attributes() As AttributeCollection
        Get
            If (Me.m_objAttributes Is Nothing) Then
                Me.m_objAttributes = New AttributeCollection(New StateBag(True))
            End If
            Return Me.m_objAttributes
        End Get
    End Property

    Public Function GetAttribute(ByVal key As String) As String Implements System.Web.UI.IAttributeAccessor.GetAttribute
        Return Me.Attributes.Item(key)
    End Function

    Public Sub SetAttribute(ByVal key As String, ByVal value As String) Implements System.Web.UI.IAttributeAccessor.SetAttribute
        Me.Attributes.Item(key) = value
    End Sub
End Class

Public Class VirtualPageBuilder
    Inherits ControlBuilder

End Class